<template>
  <div>

    <div style="margin-bottom: 5px;"><!--按钮区-->
      <el-input v-model="name" placeholder="请输入省份名" suffix-icon="el-icon-search" style="width: 200px;"
        @keyup.enter.native="loadPost"></el-input>
      <el-button type="primary" style="margin-left: 5px;" @click="loadPost">查询</el-button>
      <el-button type="success" @click="resetParam">重置</el-button>

      <el-button type="primary" style="margin-left: 5px;" @click="add">新增</el-button>

      <!--   excel导出   -->
      <el-form ref="exelForm" :inline="true">
        <el-form-item prop="explodexel">
          <download-excel class="export-excel-wrapper" :data="tableData" :fields="json_fields" name="省份疫情数据表.xls">
            <el-button type="primary" size="small">导出EXCEL</el-button>
          </download-excel>
        </el-form-item>

      </el-form>

    </div>

    <!--表格区    -->
    <el-table :data="tableData" :header-cell-style="{ background: '#f2f5fc', color: '#555555' }" border>
      <el-table-column prop="id" label="ID" width="50">
      </el-table-column>
      <el-table-column prop="name" label="省份名" width="120">
      </el-table-column>
      <el-table-column prop="curedCount" label="当前确诊人数" width="120">
      </el-table-column>
      <el-table-column prop="confirmedCount" label="累计确诊人数" width="120">
      </el-table-column>
      <el-table-column prop="curedCount" label="治愈人数" width="120">
      </el-table-column>
      <el-table-column prop="deadCount" label="死亡人数" width="120">
      </el-table-column>
      <el-table-column prop="highDangerCount" label="高风险地区数量" width="140">
      </el-table-column>
      <el-table-column prop="midDangerCount" label="中风险地区数量" width="140">
      </el-table-column>
      <el-table-column prop="time" label="时间" width="140">
      </el-table-column>

      <!-- 表格里的按钮   -->
      <el-table-column prop="operate" label="操作" key="slot">
        <template #default="scope">

          <el-button size="small" type="success" @click="mod(scope.row)">编辑</el-button>
          <el-popconfirm title="确定删除吗？" @confirm="del(scope.row.id)" style="margin-left: 5px;">
            <el-button slot="reference" size="small" type="danger">删除</el-button><!-- @confirm确认后删除-->
          </el-popconfirm>

        </template>
      </el-table-column>
    </el-table>

    <!--  分页模块  -->
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageNum"
      :page-sizes="[5, 10, 20, 30]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

    <!--  弹窗  -->
    <el-dialog title="提示" :visible.sync="centerDialogVisible" width="30%" center>
      <el-form ref="form" :rules="rules" :model="form" label-width="150px">
        <el-form-item label="省份名" prop="name"><!--这里的prop是和rule相对应的-->
          <el-col :span="20">
            <el-input v-model="form.name"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="当前确诊人数" prop="currentConfirmedCount">
          <el-col :span="20">
            <el-input v-model="form.currentConfirmedCount"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="累计确诊人数" prop="confirmedCount">
          <el-col :span="20">
            <el-input v-model="form.confirmedCount"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="治愈人数" prop="curedCount">
          <el-col :span="20">
            <el-input v-model="form.curedCount"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="死亡人数" prop="deadCount">
          <el-col :span="20">
            <el-input v-model="form.deadCount"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="高风险地区数量" prop="highDangerCount">
          <el-col :span="20">
            <el-input v-model="form.highDangerCount"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="中风险地区数量" prop="midDangerCount">
          <el-col :span="20">
            <el-input v-model="form.midDangerCount"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="时间" prop="time">
          <el-col :span="20">
            <el-input v-model="form.time"></el-input>
          </el-col>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>
<script>
import { getToken } from '@/utils/auth'
export default {
  name: "Main",
  data() {
    /* const item={
       daata:'2020-10-26',
       name:'纳西妲',
     };*/
    return {
      tableData: [],
      json_fields: {
        ID: "id",
        省份名称: "name",
        当前确诊人数: "currentConfirmedCount",
        累计确诊人数: "confirmedCount",
        治愈人数: "curedCount",
        死亡人数: "deadCount",
        高风险区数量: "highDangerCount",
        中风险区数量: "midDangerCount",
        时间: "time",
      },
      json_meta: [
        [
          {
            key: "charset",
            value: "utf-8"
          }
        ]
      ],
      pageSize: 5,
      pageNum: 1,
      total: 0,
      name: '',
      centerDialogVisible: false,//默认弹窗不打开
      form: {//这里是弹窗的绑定
        id: '',
        name: '',
        currentConfirmedCount: '',
        confirmedCount: '',
        curedCount: '',
        deadCount: '',
        highDangerCount: '',
        midDangerCount: '',
        time: '',
      },
      rules: {
        name: [
          { required: true, message: '请输入省份名', trigger: 'blur' }
        ],
        currentConfirmedCount: [
          { required: true, message: '请输入当前确诊人数', trigger: 'blur' },
          { pattern: /^([0-9][0-9]*){1,4}$/, message: '数量必须为正整数字', trigger: "blur" },
        ],
        confirmedCount: [
          { required: true, message: '请输入累计确诊人数', trigger: 'blur' },
          { pattern: /^([0-9][0-9]*){1,4}$/, message: '数量必须为正整数字', trigger: "blur" },
        ],
        curedCount: [
          { required: true, message: '请输入治愈人数', trigger: 'blur' },
          { pattern: /^([0-9][0-9]*){1,4}$/, message: '数量必须为正整数字', trigger: "blur" },
        ],
        deadCount: [
          { required: true, message: '请输入死亡人数', trigger: 'blur' },
          { pattern: /^([0-9][0-9]*){1,4}$/, message: '数量必须为正整数字', trigger: "blur" },
        ],
        highDangerCount: [
          { required: true, message: '请输入高风险区数量', trigger: 'blur' },
          { pattern: /^([0-9][0-9]*){1,4}$/, message: '数量必须为正整数字', trigger: "blur" },
        ],
        midDangerCount: [
          { required: true, message: '请输入中风险区数量', trigger: 'blur' },
          { pattern: /^([0-9][0-9]*){1,4}$/, message: '数量必须为正整数字', trigger: "blur" },
        ],

      }


    }
  }, methods: {
    //有关分页的方法
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageNum = 1//防止修改当前页时不会跳到第一页
      this.pageSize = val
      this.loadPost()//实现跳转当前页
    },
    //翻页的方法
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum = val
      this.loadPost()//实现点击页码也可以跳转到对应页数
    },
    loadGet() {
      this.$axios.get(this.$httpUrl + '/province/list', {headers: {'token': getToken()}}).then(res => res.data).then(res => {
        console.log(res);
      })
    },
    loadPost() {//调用后端的分页查询
      this.$axios.post(this.$httpUrl + '/province/listPage', {
        pageSize: this.pageSize,
        pageNum: this.pageNum,
        param: {
          name: this.name//将输入框内的数据获取
        }
      }, {headers: {'token': getToken()}}).then(res => res.data).then(res => {
        console.log(res);
        if (res.code == 200) {
          this.tableData = res.data//获取封装数据中的data,prop和这里获取到的名称要一一对应
          this.total = res.total
        } else {
          alert('获取数据失败')
        }

      })
    },
    //实现重置搜索框内容
    resetParam() {
      this.name = '';
    },
    //重置表单,如果不在rule里面的列，就无法生效
    resetForm() {
      this.$refs.form.resetFields();
    },

    //新增按钮的新增方法
    add() {
      this.centerDialogVisible = true;
      this.$nextTick(() => {
        this.resetForm();//重置表单
      })

    },
    mod(row) {
      console.log(row);

      this.centerDialogVisible = true;

      this.$nextTick(() => {//异步的效果,防止内容残留在表单上
        //将该行的数据传递到表单上
        this.form.id = row.id//不显示，但是还要获取，为了判断是新增还是修改
        this.form.name = row.name
        this.form.currentConfirmedCount = row.currentConfirmedCount
        this.form.confirmedCount = row.confirmedCount
        this.form.curedCount = row.curedCount
        this.form.deadCount = row.deadCount
        this.form.highDangerCount = row.highDangerCount
        this.form.midDangerCount = row.midDangerCount
        this.form.time = row.time
      })


    },
    del(id) {
      console.log(id)

      this.$axios.get(this.$httpUrl + '/province/delete?id=' + id, {headers: {'token': getToken()}}).then(res => res.data).then(res => {
        console.log(res);
        if (res.code == 200) {
          this.$message({
            message: '删除成功！',
            type: 'success'
          });
          this.loadPost()//刷新一下列表,让新的数据显示在列表中
        } else {
          this.$message({
            message: '删除失败！',
            type: 'error'
          });
        }

      })



    },
    doSave() {
      this.$axios.post(this.$httpUrl + '/province/save', this.form, {headers: {'token': getToken()}}).then(res => res.data).then(res => {
        console.log(res);
        if (res.code == 200) {
          this.$message({
            message: '新增成功！',
            type: 'success'
          });
          this.centerDialogVisible = false//成功之后关闭弹框
          this.loadPost()//刷新一下列表,让新的数据显示在列表中
          this.resetForm();//重置表单

        } else {
          this.$message({
            message: '操作失败！',
            type: 'error'
          });
        }

      })

    },
    doMod() {
      this.$axios.post(this.$httpUrl + '/province/update', this.form, {headers: {'token': getToken()}}).then(res => res.data).then(res => {
        console.log(res);
        if (res.code == 200) {
          this.$message({
            message: '操作成功！',
            type: 'success'
          });
          this.centerDialogVisible = false//成功之后关闭弹框
          this.loadPost()//刷新一下列表,让新的数据显示在列表中
          this.resetForm();//重置表单

        } else {
          this.$message({
            message: '操作失败！',
            type: 'error'
          });
        }

      })

    },

    save() {//弹窗的确认键的方法，用来提交数据到后台

      this.$refs.form.validate((valid) => {//如果出错，就不能执行下去
        if (valid) {
          if (this.form.id) {//判断是修改还是新增
            this.doMod();
          } else {
            this.doSave();
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },


  },
  beforeMount() {
    //this.loadGet();
    this.loadPost();

  }


}
</script>

<style scoped>

</style>